import { message } from 'antd';
import axios, { AxiosResponse } from 'axios';
import { baseURL } from '../config/configFile';
import Response from '../types/Response';

// axios实例
const axiosInstance = axios.create({
    baseURL
})

//登录请求
export async function loginRequest(name: string, password: string): Promise<Boolean> {
    try {
        const { data }: AxiosResponse<Response<null>> = await axiosInstance.post('/login', { name, password });
        if (!data.success) message.warn(data.message);  //登陆失败弹窗
        return !!data.success;
    }
    catch (e) {
        message.error('网络错误');
        return false;
    }
}

//检查是否已经登录
export async function checkLogin(): Promise<Boolean> {
    try {
        const { data }: AxiosResponse<Response<null>> = await axiosInstance.get('/checkLogin');
        return !!data.success;
    }
    catch (e) {
        message.error('网络错误');
        return false;
    }
}